﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ToolsLayer;
using Webcar.BLL;

namespace Webcar.Car
{
    public partial class RunRecord : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GridViewStyler gvs = new GridViewStyler(this.GridView1);
            gvs.SetStyle();
            if (!this.IsPostBack)
            {
                this.SearchPanel1.BindColumn("runrecord.xml");
                this.SearchPanel1.OriginSql = this.GetSQL();
                this.SearchPanel1.SetQuery(this.GetQueryStringCondition());
                this.BindInfo();
                this.HyperLink1.NavigateUrl = this.AddReturnUrl("RunRecordEdit.aspx");
            }
        }

        private string GetSQL()
        {
            string sql = @"select * from v_run_record a where a.dept_code in "+this.GetDeptSQL();
            return sql;

        }

        private void BindInfo()
        {
            this.WebPager1.ItemsPerPage = 13; //每页显示的记录条数。
            if (this.SearchPanel1.GetSort() == "")
                this.WebPager1.SortField = "run_date_s";
            else
                this.WebPager1.SortField = this.SearchPanel1.GetSort(); //用于排序的字段。
            this.WebPager1.Sortmode = DevCenterNew.SortMode.DESC;
            if (this.SearchPanel1.GetSortMode().ToUpper() == "DESC")
                this.WebPager1.Sortmode = DevCenterNew.SortMode.DESC;
            else if (this.SearchPanel1.GetSortMode().ToUpper() == "ASC")
                this.WebPager1.Sortmode = DevCenterNew.SortMode.ASC;
            this.WebPager1.ControlToPaginate = "GridView1"; //与其关联需绑定的DataGrid或DataList控件ID。

            this.WebPager1.SelectCommand = this.SearchPanel1.GetQuerySql();
            this.WebPager1.ConnectionString = DB.ConnectString;
            this.WebPager1.DataBind();
        }
        protected void Seach_Click(object sender, EventArgs e)
        {
            //重置SQL
            this.SearchPanel1.OriginSql = this.GetSQL();
            this.BindInfo();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
                return;
            string send_no = this.GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

            HyperLink hl = (HyperLink)e.Row.FindControl("linkEdit");
            hl.NavigateUrl = this.AddReturnUrl("RunRecordEdit.aspx?id=" + send_no);
            //HyperLink hl2 = (HyperLink)e.Row.FindControl("linkView");
            //hl2.NavigateUrl = this.AddReturnUrl("RunRecordEdit.aspx?id=" + send_no);

            this.BindQueryLink(e.Row, "linkDeptName", "dept_name");
            this.BindQueryLink(e.Row, "linkRecordNo", "record_no");

            /*InfoChecker ic = new InfoChecker();

            if (ic.InfoIsRead(send_no, InfoCheckType.CarSend))
            {
                Image im = (Image)e.Row.FindControl("Image1");
                im.Visible = false;
            }
            //清除掉过长的列
            
            s_temp = PublicFunction.GetGridCellText(e.Row, "lblReason");
            if (s_temp.Length > 18)
                PublicFunction.SetGridCellText(e.Row, "lblReason", s_temp.Substring(0, 18) + "…");
            */
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sendno = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
            Response.Redirect(this.AddReturnUrl("RunRecordEdit.aspx?flag=delete&id=" + sendno));
        }

        protected void linkPrint_Click(object sender, EventArgs e)
        {
            this.OpenPrintWindow(this.SearchPanel1.GetQuerySql(), "~/car/runrecord.xml");
        }
    }
}
